const await = require('await-stream-ready/lib/await');
let { Service } = require('egg')

const fs = require('fs')
const path = require('path');
// 异步二进制 写入流
const awaitWriteStream = require('await-stream-ready').write;
// 管道读入一个虫洞。
const sendToWormhole = require('stream-wormhole');

class UploadService extends Service {
  async imgUpload() {
    let { mysql } = this.app
  }
  // 获取图片
  async query(params) {
    const { app } = this;
    try {
      const result = await app.mysql.get('img', params);
      return result;
    } catch (error) {
      console.log(error);
      return null;
    }
  }
  // 增加图片
  async add(params) {
    const { app } = this;
    try {
      const result = await app.mysql.insert('img', params);
      return result.insertId;
    } catch (error) {
      return null;
    }
  }

  // 通过excel将用户数据上传到mysql表中
  async ExcelUpload(trueList, roleId) {
    let { mysql } = this.app

    if (roleId == 2) {
      for (let i = 0; i < trueList.length; i++) {
        let studentId = trueList[i]["学号"]
        let stuName = trueList[i]["姓名"]
        let stuAge = trueList[i]["年龄"]
        let stuGender = trueList[i]["性别"]
        try {
          let res = await mysql.insert('student', { studentId, stuName, stuAge, stuGender })
        } catch {
          continue
        }
      }

    } else if (roleId == 1) {
      for (let i = 0; i < trueList.length; i++) {
        let teacherId = trueList[i]["工号"]
        let teachName = trueList[i]["姓名"]
        let teachAge = trueList[i]["年龄"]
        let teaGender = trueList[i]["性别"]

        try {

          let res = await mysql.insert('teacher', { teacherId, teachName, teachAge, teaGender })
        } catch {
          continue
        }
      }
    }else if(roleId == 0){
      for (let i = 0; i < trueList.length; i++) {
        let adminId = trueList[i]["工号"]
        let admName = trueList[i]["姓名"]
        let admAge = trueList[i]["年龄"]
        let admGender = trueList[i]["性别"]
        try {
          let res = await mysql.insert('admin', { adminId, admName, admAge, admGender })
        } catch {
          continue
        }
      }
    }
    return "上传成功"



  }


}


module.exports = UploadService